327C - Magic Five - CodeForces Solution


combinatorics math *1700

Please click on ads to support us..

C++ Code:

#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long int
ll ans=0;

ll po(ll x, ll y, ll ii){
	ll k, i, j; k=1;
	while(y>0){
		if(y%2==1){
			k=(k*x)%ii;
		} y=y/2; x=(x*x)%ii;
	} return k;
}

int main(){
	ll n, i, k, x, y, p, q, j, ii, jj, t, a[200010];
	char s[200010]; ii=1e9+7;
	scanf("%s", s); n=strlen(s);
	scanf("%lld", &k); y=1;
	for(i=0; i<n; i++){
		if(s[i]=='0' || s[i]=='5'){
			x=po(2, i+n*k, ii);
			p=po(2, n, ii);
			p=po(p-1, ii-2, ii);
			q=(x-y)%ii; q=(q*p)%ii; if(q<0) q+=ii;
			ans=(ans+q)%ii;
		} y=(y*2)%ii;
	}
	printf("%lld\n", ans);
	return 0;
}


Comments

Submit
0 Comments
More Questions

151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother